import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

// https://vite.dev/config/
export default defineConfig(({ command, mode }) => {
  const isDev = command === 'serve'
  
  return {
    plugins: [vue()],
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src')
      }
    },
    define: {
      global: 'globalThis',
      __IS_DEV__: isDev
    },
    base: './', // 使用相对路径，确保在Electron中正确加载资源
    build: {
      outDir: 'dist',
      assetsDir: 'assets',
      sourcemap: isDev,
      rollupOptions: {
        output: {
          manualChunks: {
            'vendor': ['vue', 'vue-router', 'pinia'],
            'ui': ['naive-ui'],
            'editor': ['@codemirror/view', '@codemirror/state']
          }
        }
      }
    },
    server: {
      host: '0.0.0.0',
      port: 5173,
      strictPort: true, // 使用固定端口，避免端口冲突
      cors: true,
      hmr: {
        port: 5173 // 确保HMR端口与服务器端口一致
      }
    },
    optimizeDeps: {
      exclude: ['electron'] // 排除electron，避免在浏览器环境中打包
    }
  }
})
